
/**
 * Autocomplete para pesquisa de Pessoas por Nome
 */
$(function() {
	$("#LoanPersonName").autocomplete(
	{
		source: "autoComplete",
		minLenght: 2,
		select: function(event, ui) {
			var selectedObj = ui.item;
			$("#LoanPersonName").val(selectedObj.label);
			$("#LoanPersonId").val(selectedObj.value);
			return false;
		}
	});
});

/**
 * Adiciona as Tabs ao form Detail
 */
$(function() {
	$( "#tabs" ).tabs();
});

/**
 * 
 */
$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	
	var name = $( "#copy_id" ),
		email = $( "#email" ),
		password = $( "#password" ),
		allFields = $( [] ).add( name ).add( email ).add( password ),
		tips = $( ".validateTips" );

	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}

	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
				min + " and " + max + "." );
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		} else {
			return true;
		}
	}
	
	$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 250,
		width: 350,
		modal: true,
		buttons: {
			"OK": function() {		
					// Adiciona o item a tabela de exemplares
					$( "#users tbody" ).append( "<tr>" +
						"<td>" + name.val() + "</td>" + 
						"<td>" + email.val() + "</td>" + 
						"<td>" + password.val() + "</td>" +
					"</tr>" );
					
					// Adiciona um input hidden com os dados do exemplar
					$( "#form" ).append( "<input " +
					"type=\"hidden\" id=\"CopyCopy\" " +
					"value=\"" + name.val() + "\" name=\"data[Copy][Copy][]\" " +
					">" );
					$( this ).dialog( "close" );
				
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});

	// Chamada do Dialog
	$( "#add" )
		.button()
		.click(function() {
			$( "#dialog-form" ).dialog( "open" );
		});
});